<%@ page language="java" contentType="text/html; charset=utf-8" %>
<jsp:useBean id="rs" class="weaver.conn.RecordSet" scope="page"/>
<jsp:useBean id="bb" class="weaver.general.BaseBean"/>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="okhttp3.*" %>
<%@ page import="org.bouncycastle.jce.provider.BouncyCastleProvider" %>
<%@ page import="weaver.common.DateUtil" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.rsa.security.Base64" %>
<%@ page import="javax.crypto.Cipher" %>
<%@ page import="javax.crypto.spec.IvParameterSpec" %>
<%@ page import="javax.crypto.spec.SecretKeySpec" %>
<%@ page import="java.math.BigInteger" %>
<%@ page import="java.security.MessageDigest" %>
<%@ page import="java.security.Security" %>
<%@ page import="java.security.spec.AlgorithmParameterSpec" %>
<%@ page import="java.util.concurrent.TimeUnit" %>
<%

    String currentDate = DateUtil.getCurrentDate();
    JSONObject paramJson = new JSONObject();
    try {
        String sign = Util.null2String(getSign());
        out.print(">>>>>sign=" + sign + "<br/>");
        paramJson.put("sign", sign);
        paramJson.put("sta_date", currentDate);
        paramJson.put("end_date", currentDate);
        paramJson.put("requestId", "");
        out.print(">>>>>请求参数=" + paramJson + "<br/>");
        JSONObject resultJson = sendOKHttpRequest(paramJson);
        out.print(">>>>>返回结果=" + resultJson + "<br/>");
    } catch (Exception e) {
        out.print(">>>>>生成签名异常");
        e.printStackTrace();
    }

%>
<%!

    private static BaseBean baseBean = new BaseBean();
    //APPID
    private static final String APPID = "614xc";
    //APPSECRET
    private static final String APPSECRET = "85errldds56assd";
    //请求地址
    private static final String URL = "http://financial.camci.com.cn/api/webTravel";
    //编码格式
    private static final String CHARSET_NAME = "UTF-8";
    // AES加密
    private static final String AES_NAME = "AES";
    // 加密模式
    private static final String ALGORITHM = "AES/CBC/PKCS7Padding";
    // 密钥
    private static final String SECRETKEY = "8db7fba29839424d";
    // 偏移量
    private static final String IV = "8db7fba29839424d";


    static {

        Security.addProvider(new BouncyCastleProvider());

    }

    /**
     * 加密
     *
     * @param content
     * @return
     */
    private static String encrypt(String content) {
        byte[] result = null;
        try {
            baseBean.writeLog(">>>>>encrypt>>>ALGORITHM" + ALGORITHM);
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            baseBean.writeLog(">>>>>encrypt>>>AES_NAME" + AES_NAME);
            baseBean.writeLog(">>>>>encrypt>>>CHARSET_NAME" + CHARSET_NAME);
            SecretKeySpec keySpec = new SecretKeySpec(SECRETKEY.getBytes(CHARSET_NAME), AES_NAME);
            baseBean.writeLog(">>>>>encrypt>>>IV" + IV);
            AlgorithmParameterSpec paramSpec = new IvParameterSpec(IV.getBytes());
            cipher.init(Cipher.ENCRYPT_MODE, keySpec, paramSpec);
            result = cipher.doFinal(content.getBytes(CHARSET_NAME));
            baseBean.writeLog(">>>>>encrypt>>>SECRETKEY" + SECRETKEY);
        } catch (Exception e) {
            baseBean.writeLog(">>>>>encrypt>>>加密失败" + e);
            e.printStackTrace();
        }
        return Base64.encodeBase64String(result);
    }

    /**
     * 获取签名
     *
     * @return
     */
    public static String getSign() {
        JSONObject paramJson = new JSONObject();
        paramJson.put("appId", APPID);
        paramJson.put("appSecret", APPSECRET);
        paramJson.put("timestamp", (System.currentTimeMillis() / 1000));
        String content = paramJson.toJSONString();// 原文信息
        baseBean.writeLog(">>>>>getSign>>>content" + content);
        return encrypt(content);
    }

    /**
     * 请求处理器
     *
     * @param paramsJson
     * @return
     */
    public static JSONObject sendOKHttpRequest(JSONObject paramsJson) {
        BaseBean baseBean = new BaseBean();
        baseBean.writeLog(">>>>>sendOKHttpRequest>>>>>paramsJson="+paramsJson);
        JSONObject resultJson = new JSONObject();
        String code = "failure";
        String message = "";
        String data = "";
        try {
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .connectTimeout(60, TimeUnit.SECONDS)
                    .readTimeout(60, TimeUnit.SECONDS)
                    .writeTimeout(60, TimeUnit.SECONDS)
                    .build();
            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, paramsJson.toJSONString());
            Request request = new Request.Builder()
                    .url(URL)
                    .method("POST", body)
                    .addHeader("Content-Type", "application/json")
                    .build();
            Response response = client.newCall(request).execute();
            String result = Util.null2String(response.body().string());
            baseBean.writeLog(">>>>>sendOKHttpRequest>>>>>result="+result);
            if (!"".equals(result)) {
                data = result;
                code = "success";
            } else {
                message = "response result is null";
            }
        } catch (Exception e) {
            message = "response timeout";
            e.printStackTrace();
        }
        resultJson.put("code", code);
        resultJson.put("data", data);
        resultJson.put("message", message);
        return resultJson;
    }

    /**
     * 字符串生成MD5
     * @param plainText
     * @return
     */
    public static String stringToMD5(String plainText) {
        byte[] secretBytes = null;
        try {
            secretBytes = MessageDigest.getInstance("md5").digest(
                    plainText.getBytes());
        } catch (Exception e) {
            throw new RuntimeException("没有这个md5算法！");
        }
        String md5code = new BigInteger(1, secretBytes).toString(16);
        for (int i = 0; i < 32 - md5code.length(); i++) {
            md5code = "0" + md5code;
        }
        return md5code;
    }


%>
